<?php
/**************
* DESCRIPTION *
***************
* Return or register the question and a response of a user
*
* Usage :
* -------
* call get() method to get question and response of a user
* call set() method to register the question and the response of a user
*
*/
	include_once "../library/DbConnection.php";
	include_once "../globals.php";

	class UserContent
	{
		private $db;



		/****************
		** CONSTRUCTOR **
		****************/
		public function __construct()
		{
			session_start();
			$this->db = new DbConnection($GLOBALS["db_server"], $GLOBALS["db_login"], $GLOBALS["db_pass"], $GLOBALS["db_name"]);
			$this->db->connect();
			 
		}



		/*******************
		** PUBLIC METHODS **
		*******************/

		/** Get the question and the response of the user
		* @return	{Object}	:	"question" -> The question of the user
		*					"response" -> The response of the user
		* 					"session"  -> Session state (true/false);
		**/
		public function get()
		{
			$ret		= array();
			if(!isset($_SESSION['user_id']))
				$ret["session"]	= false;
			else{
				$req		= $this->db->query("SELECT knowResponse, knowQuestion FROM logout_Users WHERE id='".$_SESSION['virtual_id']."'");
				$res		= $this->db->fetch_object($req);
				$ret["question"]	= $res->knowQuestion;
				$ret["response"]	= $res->knowResponse;
				$ret["session"]	= true;
			}
			return $ret;
		}


		/** Set the question and the response of the user
		* @param	question	{String}	:	Question
		* @param	response	{String}	:	Response of the question
		* @return			{Object}	:	"result" ::	true  -> Saved
		* 										false -> Error
		* 							"session"  ->  Session state (true/false);
		**/
		public function set($question, $response)
		{
			if(!isset($_SESSION['user_id']))
				$ret["session"]	= false;
			else{
				$res		= $this->db->query("UPDATE logout_Users SET knowQuestion='".addslashes($question)."', knowResponse='".addslashes($response)."' WHERE id='".$_SESSION['user_id']."'");
				$ret		= array();
				$ret["session"]	= true;
				$ret["result"]		= $res;
			}
			return $ret;
		}
	}
?>